package yunquan.wang.modules.customer.service.impl;

import java.util.Collection;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.dao.QueryHelper;
import yunquan.wang.base.dao.springdata.BaseRepository;
import yunquan.wang.base.service.impl.BaseServiceImpl;
import yunquan.wang.modules.customer.dao.CustomerDao;
import yunquan.wang.modules.customer.entity.Company;
import yunquan.wang.modules.customer.entity.Contacts;
import yunquan.wang.modules.customer.entity.ContactsInfo;
import yunquan.wang.modules.customer.entity.Customer;
import yunquan.wang.modules.customer.service.CompanyService;
import yunquan.wang.modules.customer.service.ContactsService;
import yunquan.wang.modules.customer.service.CustomerService;

@Service("customerService")
@Transactional(value = "jpaTransaction")
public class CustomerServiceImpl  extends BaseServiceImpl<Customer> implements
CustomerService{
	@Resource(name = "customerDao")
	private CustomerDao customerDao;
	@Resource
    private CompanyService companyService;
	@Resource
   private ContactsService  contactsService;
	
	@Override
	public BaseRepository<Customer, String> getDao() {
		return customerDao;
	}
	@Override
	public Customer save(Customer entity, Set<Contacts> contancts) {
		Company company=	companyService.save(entity.getCompany());
		entity.setCompany(company);
		entity=	super.save(entity);
		for(Contacts contacs:contancts)
		{
			contacs.setCustomer(entity);
			contacs.setContactsInfo(new ContactsInfo() );
			contactsService.save(contacs);
		}
	
		return entity;
	}
	public Page<Customer> findPage(Pageable page, Collection<PropertyFilter> filters) {
		Specification specification=QueryHelper.listFilter(filters);
		return getDao().findAll(specification, page);
	}

}
